package com.probox.common.entity.service.customer.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.activity.api.domain.ActivityCouponVo;
import com.probox.activity.api.domain.CouponQuery;
import com.probox.common.core.domain.WeChatInfomationCoupon;
import com.probox.common.core.domain.WechatInfomation;
import com.probox.common.core.enums.*;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.redis.service.RedisService;
import com.probox.customer.api.domain.CustomerInfoAccount;
import com.probox.customer.api.domain.CustomerInfoVo;
import com.probox.common.entity.domain.customer.CustomerOperation;
import com.probox.common.entity.domain.customer.CustomerOperationPrivilege;
import com.probox.common.entity.domain.customer.CustomerOperationPrivilegeGive;
import com.probox.common.entity.mapper.customer.CustomerInfoAccountMapper;
import com.probox.common.entity.mapper.customer.CustomerOperationMapper;
import com.probox.common.entity.mapper.customer.CustomerOperationPrivilegeGiveMapper;
import com.probox.common.entity.mapper.customer.CustomerOperationPrivilegeMapper;
import com.probox.common.entity.service.customer.ICustomerGroupService;
import com.probox.common.entity.service.customer.ICustomerOperationService;
import com.probox.common.entity.vo.customer.CustomerOperationPrivilegeGiveVo;
import com.probox.common.entity.vo.customer.CustomerOperationPrivilegeVo;
import com.probox.common.entity.vo.customer.CustomerOperationVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 客户运营Service业务层处理
 *
 * @author usoft
 * @date 2021-07-23
 */
@Service
@Transactional
public class CustomerOperationServiceImpl extends ServiceImpl<CustomerOperationMapper, CustomerOperation> implements ICustomerOperationService {
    @Autowired
    private CustomerOperationMapper customerOperationMapper;
    @Autowired
    private CustomerOperationPrivilegeMapper customerOperationPrivilegeMapper;

    @Autowired
    private CustomerOperationPrivilegeGiveMapper customerOperationPrivilegeGiveMapper;

    @Autowired
    private CustomerInfoAccountMapper  customerInfoAccountMapper;

    @Autowired
    private ICustomerGroupService customerGroupService;




    @Autowired
    RedisService redisService;




    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 查询客户运营
     *
     * @param operationUid 客户运营ID
     * @return 客户运营
     */
    @Override
    public CustomerOperationVo selectCustomerOperationById(Long operationUid) {
        CustomerOperationVo customerOperationVo =  customerOperationMapper.selectOperationById(operationUid);
        List<CustomerOperationPrivilegeVo> customerOperationPrivilegeVoList = customerOperationVo.getCustomerOperationPrivilegeVoList();
        for (CustomerOperationPrivilegeVo operationPrivilegeVo:customerOperationPrivilegeVoList){
            List<CustomerOperationPrivilegeGiveVo> customerOperationPrivilegeGivesvo = operationPrivilegeVo.getCustomerOperationPrivilegeGivesvo();
            for (CustomerOperationPrivilegeGiveVo privilegeGiveVo:customerOperationPrivilegeGivesvo){
                ActivityCouponVo coupon = getCouponById(privilegeGiveVo.getGiveUid());
                //更新优惠券总数
                if(coupon!=null){
                    privilegeGiveVo.setCouponDesc(coupon.getCouponDesc());
                    privilegeGiveVo.setCouponNums(coupon.getCouponNums());
                    privilegeGiveVo.setCouponName(coupon.getCouponName());
                }

            }
        }
        return customerOperationVo;
    }

    /**
     * 查询客户运营列表
     *
     * @param customerOperation 客户运营
     * @return 客户运营
     */
    @Override
    public List<CustomerOperation> queryList(CustomerOperation customerOperation) {
        QueryWrapper<CustomerOperation> queryWrapper = Wrappers.query();
//        if (StringUtils.isNotEmpty(String.valueOf(customerOperation.getOperationUid())) && customerOperation.getOperationUid() != null) {
//            queryWrapper.eq("OPERATION_UID", customerOperation.getOperationUid());
//        }
        queryWrapper.eq("PLATFORM_ID", SecurityUtils.getPlatformId());
        queryWrapper.eq("OPERATION_TYPE", "0");
        queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
        queryWrapper.orderByDesc("CREATE_TIME");
        return customerOperationMapper.selectList(queryWrapper);
    }

    /**
     * 新增客户运营
     *
     * @param customerOperationVo 客户运营
     * @return 结果
     */
    @Override
    public boolean insertCustomerOperation(CustomerOperationVo customerOperationVo) {
        //公共参数
        DateTime date = DateUtil.date();
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getUsername();
        Long platformId = SecurityUtils.getPlatformId();
        //新增客户运营
        CustomerOperation customerOperation = new CustomerOperation();
        customerOperation.setPlatformId(platformId);
        customerOperation.setDefinitionName(customerOperationVo.getDefinitionName());
        customerOperation.setOperationType(customerOperationVo.getOperationType());
        customerOperation.setDefinitionUid(customerOperationVo.getDefinitionUid());
        customerOperation.setOperationTypeValue(customerOperationVo.getOperationTypeValue());
        customerOperation.setOperationName(customerOperationVo.getOperationName());
        customerOperation.setOperationMode(customerOperationVo.getOperationMode());
        //运营状态
        String operationBeginTime = customerOperationVo.getOperationBeginTime();
        String operationEndTime = customerOperationVo.getOperationEndTime();
        Date beginDate = DateUtil.parse(operationBeginTime);
        Date endDate = DateUtil.parse(operationEndTime);
        Date newDate = new Date();
        // -1 小于 1大于 0等于
        int begin = 0;
        if(beginDate != null){
            begin = beginDate.compareTo(newDate);
        }
        int end = -2;
        if (endDate != null){
            end = endDate.compareTo(newDate);
        }
        if (CustomerOperationTimeTypeEnum.timingTermTime.getKey().equals(customerOperationVo.getOperationMode())){
            customerOperation.setExecuteType(customerOperationVo.getExecuteType());
            if (CustomerOperationExecuteTypeEnum.regularlyPerform.getKey().equals(customerOperationVo.getExecuteType())){
                customerOperation.setOperationBeginTime(customerOperationVo.getOperationBeginTime());
            }else{
                operationBeginTime = String.valueOf(DateUtil.date());
                customerOperation.setOperationBeginTime(String.valueOf(DateUtil.date()));
            }
            customerOperation.setOperationTimeType(CustomerOperationTimeTypeEnum.timingTermTime.getKey());
            if (begin == -1){
                //未开始
                customerOperation.setOperationStatus(CustomerOperationStatusEnum.notStart.getKey());
            }else {
                //已开始
                customerOperation.setOperationStatus(CustomerOperationStatusEnum.underway.getKey());
            }
        }else {

            customerOperation.setOperationBeginTime(customerOperationVo.getOperationBeginTime());
            customerOperation.setOperationEndTime(customerOperationVo.getOperationEndTime());
            customerOperation.setOperationTimeType(CustomerOperationTimeTypeEnum.longTermTime.getKey());
            if (end == -1){
                customerOperation.setOperationStatus(CustomerOperationStatusEnum.over.getKey());
            }else {
                if (begin == -1){
                    //未开始
                    customerOperation.setOperationStatus(CustomerOperationStatusEnum.notStart.getKey());
                }else {
                    //已开始
                    customerOperation.setOperationStatus(CustomerOperationStatusEnum.underway.getKey());
                }
            }
        }
        List notificationChannelArr = customerOperationVo.getNotificationChannelArr();
        String notificationChannelStr = "";
        if(notificationChannelArr != null){
            for (int i = 0; i < notificationChannelArr.size(); i++) {
                notificationChannelStr += notificationChannelArr.get(i)+"/";
            }
        }
        if(!StringUtils.isEmpty(notificationChannelStr)){
            customerOperation.setNotificationChannel(notificationChannelStr);
        }
        customerOperation.setNotificationRuleType(customerOperationVo.getNotificationRuleType());
        if (CustomerOperationNotificationRuleTypeEnum.oneNotification.getKey().equals(customerOperationVo.getNotificationRuleType())){
            customerOperation.setNotificationRule(customerOperationVo.getNotificationRule());
        }
        customerOperation.setNotificationTime(customerOperationVo.getNotificationTime());
        customerOperation.setAbnormalRemind(customerOperationVo.getAbnormalRemind());
        customerOperation.setRemindPhone(customerOperationVo.getRemindPhone());

        customerOperation.setCreateName(username);
        customerOperation.setCreateTime(date);
        customerOperation.setCreateUser(userId);
        customerOperation.setDataStatus(DataStatus.valid.getKey());
        this.save(customerOperation);
        //新增运营权益
        List<CustomerOperationPrivilegeVo> customerOperationPrivilegeVoList = customerOperationVo.getCustomerOperationPrivilegeVoList();
        if(customerOperationPrivilegeVoList != null){
            for (CustomerOperationPrivilegeVo operationPrivilegeVo:customerOperationPrivilegeVoList){
                CustomerOperationPrivilege customerOperationPrivilege = new CustomerOperationPrivilege();
                customerOperationPrivilege.setOperationUid(customerOperation.getOperationUid());
                customerOperationPrivilege.setPrivilegeLevelRule(operationPrivilegeVo.getPrivilegeLevelRule());
                customerOperationPrivilege.setPrivilegeLevelInfo(operationPrivilegeVo.getPrivilegeLevelInfo());
                customerOperationPrivilege.setIsFreeshipping(operationPrivilegeVo.getIsFreeshipping());
                customerOperationPrivilege.setDiscount(operationPrivilegeVo.getDiscount());
                customerOperationPrivilege.setMultiplepoints(operationPrivilegeVo.getMultiplepoints());
                customerOperationPrivilege.setPoints(operationPrivilegeVo.getPoints());
                customerOperationPrivilege.setIsGiveCoupon(operationPrivilegeVo.getIsGiveCoupon());
                customerOperationPrivilege.setCouponNumber(operationPrivilegeVo.getCouponNumber());
                customerOperationPrivilege.setOperationPrivilegeStatus("1");
                customerOperationPrivilege.setCreateName(username);
                customerOperationPrivilege.setCreateUser(userId);
                customerOperationPrivilege.setCreateTime(date);
                customerOperationPrivilege.setDataStatus(DataStatus.valid.getKey());
                customerOperationPrivilegeMapper.insert(customerOperationPrivilege);
                if (operationPrivilegeVo.getIsGiveCoupon()==true){
                    //赠送优惠卷
                    List<CustomerOperationPrivilegeGiveVo> customerOperationPrivilegeGivesVo = operationPrivilegeVo.getCustomerOperationPrivilegeGivesvo();
                    for (CustomerOperationPrivilegeGiveVo operationPrivilegeGiveVo:customerOperationPrivilegeGivesVo){
                        CustomerOperationPrivilegeGive customerOperationPrivilegeGive = new CustomerOperationPrivilegeGive();
                        customerOperationPrivilegeGive.setOperationPrivilegeUid(customerOperationPrivilege.getOperationPrivilegeUid());
                        customerOperationPrivilegeGive.setGiveUid(operationPrivilegeGiveVo.getGiveUid());
                        customerOperationPrivilegeGive.setGiveNumber(operationPrivilegeGiveVo.getGiveNumber());
                        customerOperationPrivilegeGive.setCreateName(username);
                        customerOperationPrivilegeGive.setCreateTime(date);
                        customerOperationPrivilegeGive.setCreateUser(userId);
                        customerOperationPrivilegeGive.setDataStatus(DataStatus.valid.getKey());
                        customerOperationPrivilegeGiveMapper.insert(customerOperationPrivilegeGive);
                    }
                }
            }
        }
        //发送mq
        if(!StringUtils.isEmpty(customerOperation.getNotificationTime())){
            operationBeginTime+=" "+customerOperation.getNotificationTime();
        }
        Date operationBeginDate = DateUtil.parse(operationBeginTime);
        logger.info("新增客户运营信息发送mq,发送时间为:{},接收时间为:{}",DateUtil.date(),operationBeginDate);

        return true;
    }

    /**
     * 修改客户运营
     *
     * @param customerOperationVo 客户运营
     * @return 结果
     */
    @Override
    public boolean updateCustomerOperation(CustomerOperationVo customerOperationVo) {
        //公共参数
        DateTime date = DateUtil.date();
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getUsername();
        Long platformId = SecurityUtils.getPlatformId();
        //修改客户运营信息
        CustomerOperation customerOperation = new CustomerOperation();
        BeanUtils.copyProperties(customerOperationVo,customerOperation);
        customerOperation.setUpdateName(username);
        customerOperation.setCustomerUid(userId);
        customerOperation.setUpdateTime(date);
        customerOperationMapper.updateById(customerOperation);
        //修改客户运营权益信息
        List<CustomerOperationPrivilegeVo> customerOperationPrivilegeVoList = customerOperationVo.getCustomerOperationPrivilegeVoList();
        for (CustomerOperationPrivilegeVo operationPrivilegeVo:customerOperationPrivilegeVoList){
            CustomerOperationPrivilege customerOperationPrivilege = new CustomerOperationPrivilege();
            BeanUtils.copyProperties(operationPrivilegeVo,customerOperationPrivilege);
            customerOperationPrivilege.setUpdateName(username);
            customerOperationPrivilege.setUpdateUser(userId);
            customerOperationPrivilege.setUpdateTime(date);
            customerOperationPrivilegeMapper.updateById(customerOperationPrivilege);
            //修改客户运营权益赠送
            List<CustomerOperationPrivilegeGiveVo> customerOperationPrivilegeGivesVo = operationPrivilegeVo.getCustomerOperationPrivilegeGivesvo();
            for (CustomerOperationPrivilegeGiveVo operationPrivilegeGiveVo:customerOperationPrivilegeGivesVo){
                CustomerOperationPrivilegeGive customerOperationPrivilegeGive = new CustomerOperationPrivilegeGive();
                BeanUtils.copyProperties(operationPrivilegeGiveVo,customerOperationPrivilegeGive);
                customerOperationPrivilegeGive.setUpdateName(username);
                customerOperationPrivilegeGive.setUpdateUser(userId);
                customerOperationPrivilegeGive.setUpdateTime(date);
                customerOperationPrivilegeGiveMapper.updateById(customerOperationPrivilegeGive);
            }
        }
        return true;
    }

    /**
     * 批量删除客户运营
     *
     * @param operationUids 需要删除的客户运营ID
     * @return 结果
     */
    @Override
    public boolean deleteCustomerOperationByIds(Long[] operationUids) {
        return this.removeByIds(Arrays.asList(operationUids));
    }

    /**
     * 消费新增客户消息
     * @return
     */

    @Override
    public boolean consumptionOpearationMsg(Long operationUid) {
        CustomerOperationVo customerOperationVo =  customerOperationMapper.selectOperationById(operationUid);
        if (customerOperationVo == null) return false;
        List<CustomerOperationPrivilegeVo> customerOperationPrivilegeVoList = customerOperationVo.getCustomerOperationPrivilegeVoList();
        //更新指定人群id数量
        List<CustomerInfoVo> groupCustomerUId = customerGroupService.getGroupCustomerCount(customerOperationVo.getDefinitionUid());
        String operationBeginTime = customerOperationVo.getOperationBeginTime();
        String operationEndTime = customerOperationVo.getOperationEndTime();
        Date operationBeginDate = DateUtil.parse(operationBeginTime);
        Date operationEndDate = DateUtil.parse(operationEndTime);
        if (CustomerOperationTypeEnum.groupOperation.getKey().equals(customerOperationVo.getOperationType())){
            //判断是否为长期计划
            if (CustomerOperationTimeTypeEnum.longTermTime.getKey().equals(customerOperationVo.getOperationMode())){
                int end = moreTime(operationEndDate);
                if (end == -1){
                    //通知时间
                    String notificationTime = customerOperationVo.getNotificationTime();
                    Date date =  DateUtil.date();
                    Date newDateExcursion = DateUtil.offset(date, DateField.DAY_OF_MONTH, 1);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String newformat = sdf.format(newDateExcursion);
                    newformat+= " "+notificationTime;
                    Date newDate = DateUtil.parse(newformat);
                    //发送mq

                    logger.info("活动未结束,继续发送mq,接收时间:"+newDate);
                }
            }
            //送优惠券
            sendCoupon(groupCustomerUId,customerOperationPrivilegeVoList,operationUid);
        }
        int begin = moreTime(operationBeginDate);
        UpdateWrapper<CustomerOperation> operationUpdateWrapper = new UpdateWrapper<>();
        operationUpdateWrapper.eq("OPERATION_UID",operationUid);
        if(-1 != begin){
            operationUpdateWrapper.set("OPERATION_STATUS",CustomerOperationStatusEnum.underway.getKey());
        }else if(1 != begin){
            operationUpdateWrapper.set("OPERATION_STATUS",CustomerOperationStatusEnum.over.getKey());
        }else {
            operationUpdateWrapper.set("OPERATION_STATUS",CustomerOperationStatusEnum.notStart.getKey());
        }
        this.update(operationUpdateWrapper);
        return true;
    }

    /**
     * 给客户发送优惠券
     */
    public void sendCoupon(List<CustomerInfoVo> groupCustomerUId,List<CustomerOperationPrivilegeVo> customerOperationPrivilegeVoList,Long operationUid){
        logger.info("开始给客户赠送优惠券");
        for (CustomerOperationPrivilegeVo privilegeVo:customerOperationPrivilegeVoList){
            if (privilegeVo.getIsGiveCoupon() == true){
                List<CustomerOperationPrivilegeGiveVo> customerOperationPrivilegeGivesVo = privilegeVo.getCustomerOperationPrivilegeGivesvo();
                List<Long> couponID = new ArrayList<>();
                for (CustomerOperationPrivilegeGiveVo give:customerOperationPrivilegeGivesVo){
                    couponID.add(give.getGiveUid());
                }
                //赠送优惠券
                if(couponID.size() > 0){
                    if (groupCustomerUId.size() > 10){
                        //每次限制发送优惠券10个客户
                        int listSize=groupCustomerUId.size();
                        int toIndex = 10;
                        for(int i = 0;i<groupCustomerUId.size();i+=10){
                            if(i+10>listSize){        //作用为toIndex最后没有10条数据则剩余几条newList中就装几条
                                toIndex=listSize-i;
                            }
                            List newList = groupCustomerUId.subList(i,i+toIndex);
                            toIndex++;
                            CouponQuery couponQuery = new CouponQuery();
                            couponQuery.setCouponIds(couponID);
                            couponQuery.setOperationUid(operationUid);
                            couponQuery.setCustomerInfoVo(newList);

                        }
                    }else {
                        //少于10个客户直接发送
                        CouponQuery couponQuery = new CouponQuery();
                        couponQuery.setCouponIds(couponID);
                        couponQuery.setOperationUid(operationUid);
                        couponQuery.setCustomerInfoVo(groupCustomerUId);

                    }

                }
            }
        }
    }

    public void sendCountByCustomerUids(List customerUid){

    }


    //发放优惠券发送通知
    public void sendCouponNotification(Long couponId,String couponNumber,List<Long> customerUidList){
        logger.info("发放优惠券成功,开始发送通知,优惠券id:"+couponId+",赠送总数:"+couponNumber);
        ActivityCouponVo coupon = getCouponById(couponId);
        //给客户发送通知
        WeChatInfomationCoupon infomationCoupon = new WeChatInfomationCoupon();
        infomationCoupon.setCouponName(coupon.getCouponName());
        infomationCoupon.setCouponType(coupon.getCouponTypeName());
        String now = DateUtil.now();
        infomationCoupon.setGainTime(now);

        if (ExpireTypeEnum.receive.getKey().equals(coupon.getExpireType())){
            Date date = DateUtil.parse(now);
            DateTime expireDate = DateUtil.offsetDay(date, coupon.getExpireDays());
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(expireDate);
            infomationCoupon.setExpiresTime(dateString);
        }else{
            infomationCoupon.setExpiresTime(coupon.getEndDateStr());
        }
        infomationCoupon.setCouponNumber(couponNumber);
        for (Long infouid:customerUidList){
            logger.info("运营赠送,发送通知客户id:"+infouid);
            WechatInfomation infomation = new WechatInfomation();
//                        infomation.setPage(page);
            infomation.setData(infomationCoupon);
            QueryWrapper<CustomerInfoAccount> accountQueryWrapper = new QueryWrapper<>();
            accountQueryWrapper.eq("CUSTOMER_UID",infouid);
            List<CustomerInfoAccount> customerInfoAccountList = customerInfoAccountMapper.selectList(accountQueryWrapper);
            for (CustomerInfoAccount account:customerInfoAccountList){
                if (account.getOpenId()!=null){
                    infomation.setTouser(account.getOpenId());
                }
            }
            infomation.setInfomationType(WechatInfomationEnum.sendCoupon.getValue());
            logger.info("发送通知,优惠券名称:{},优惠卷类型：{},优惠卷赠送数:{}",coupon.getCouponName(),coupon.getCouponType(),couponNumber);

        }
    }


    //定时任务方法
//    public void occurrenceAndNotification(CustomerOperationVo customerOperationVo){
//        String key = CachePrefix.CUSTOMER_COUPONANDINFORM+customerOperationVo.getOperationUid();
//        List<CustomerInfoVo> groupCustomerUId = customerGroupService.getGroupCustomerCount(customerOperationVo.getDefinitionUid());
//        List<CustomerOperationPrivilegeVo> customerOperationPrivilegeVoList = customerOperationVo.getCustomerOperationPrivilegeVoList();
//        //发送优惠券
//        String operationBeginTime = customerOperationVo.getOperationBeginTime();
//        Date operationBeginDate = DateUtil.parse(operationBeginTime);
//        String operationEndTime = customerOperationVo.getOperationEndTime();
//        Date operationEndDate = DateUtil.parse(operationEndTime);
//        int beginVal = moreTime(operationBeginDate);
//        if (beginVal == 0){
//            redisService.setCacheObject(key,groupCustomerUId,2L, TimeUnit.DAYS);
//            sendCoupon(groupCustomerUId,customerOperationPrivilegeVoList);
//            //通知与异常提醒
//            notifications(customerOperationVo,groupCustomerUId);
//        }else {
//            List<CustomerInfoVo> newgroupCustomerUId = customerGroupService.getGroupCustomerCount(customerOperationVo.getDefinitionUid());
//            newgroupCustomerUId.removeAll(groupCustomerUId);
//            redisService.setCacheObject(key,newgroupCustomerUId,2L, TimeUnit.DAYS);
//            sendCoupon(newgroupCustomerUId,customerOperationPrivilegeVoList);
//            //通知与异常提醒
//            notifications(customerOperationVo,newgroupCustomerUId);
//        }
//
//    }

    /**
     * 通知提醒
     */
    public void notifications(CustomerOperationVo customerOperationVo,List groupCustomerUId){
        //开始时间
        String operationBeginTime = customerOperationVo.getOperationBeginTime();
        Date operationBeginDate = DateUtil.parse(operationBeginTime);
        //通知渠道
        int beginVal = moreTime(operationBeginDate);
        if (beginVal == 0){
            //在开始时间发送通知
            String notificationChannel = customerOperationVo.getNotificationChannel();
            if (!StringUtils.isEmpty(notificationChannel)){
                String[] channel = notificationChannel.split("/");
                if ("0".equals(customerOperationVo.getNotificationRuleType())){
                    //通知类型
                    String notificationRule = customerOperationVo.getNotificationRule();
                    //通知指定渠道

                    //给符合条件的客户发送通知
                }
                if ("1".equals(customerOperationVo.getNotificationRuleType())){
                    //通知所有渠道
                }
            }
        }
        //异常提醒(计划即将到期、权益库存不足和短信条数不足的情况下提醒)
        //即将到期
        String pastDueErr = null;
        //库存不足
        String understockErr = null;
        //短信条数不同
        String SmsErr = null;
        if (!StringUtils.isEmpty(customerOperationVo.getOperationEndTime())){
            //提醒过期
            String operationEndTime = customerOperationVo.getOperationEndTime();
            Date operationEndDate = DateUtil.parse(operationEndTime);
            int endVal = moreTime(operationEndDate);
            if (endVal == 0){
                pastDueErr = "当前为最后一天";
            }
        }
        //查看库存是否大于赠送数量
        if (customerOperationVo != null){
            List<CustomerOperationPrivilegeVo> customerOperationPrivilegeVoList = customerOperationVo.getCustomerOperationPrivilegeVoList();
            for (CustomerOperationPrivilegeVo privilegeVo:customerOperationPrivilegeVoList){
                if ("1".equals(privilegeVo.getIsGiveCoupon())){
                    List<CustomerOperationPrivilegeGiveVo> customerOperationPrivilegeGivesVo = privilegeVo.getCustomerOperationPrivilegeGivesvo();
                    for (CustomerOperationPrivilegeGiveVo give:customerOperationPrivilegeGivesVo){
                        ActivityCouponVo coupon = getCouponById(give.getGiveUid());
                        //判断赠送数量是否小于优惠券总数
                        if (coupon.getCouponNums() > give.getGiveNumber()){
                            understockErr = "优惠卷库存不足";
                        }
                    }
                }
            }
        }
        if ("0".equals(customerOperationVo.getAbnormalRemind())){
            //通过app推送提醒

        }
        if ("1".equals(customerOperationVo.getAbnormalRemind())){
            //通过手机号提醒
            String remindPhone = customerOperationVo.getRemindPhone();
        }


    }

    public int moreTime(Date dateTime){
        //当前时间
        Date date = DateUtil.date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String newformat = sdf.format(date);
        Date newDate = DateUtil.parse(newformat);
        //传入时间
        String format = DateUtil.format(dateTime, "yyyy-MM-dd");
        Date returnDate = DateUtil.parse(format);
        int returnVal = newDate.compareTo(returnDate);
        return returnVal;
    }

    /**
     *根据ID获取优惠券信息
     * @param couponUid
     * @return
     */
    private ActivityCouponVo getCouponById(Long couponUid){
       return null;
    }

}
